﻿using System.Data.Services;
using System.ServiceModel.Activation;
using System.Web.Mvc;
using System.Web.Routing;
using NuGet.Enterprise.Server.DataServices;
using System.Web;
using System.Globalization;
using NuGet.Enterprise.Server.Configuration;

namespace NuGet.Enterprise.Server
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication
        : HttpApplication
    {
        private static readonly NLog.Logger Log = NLog.LogManager.GetCurrentClassLogger();

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        private static void RegisterFeed(RouteCollection routes, ServiceHostFactory serviceFactory, string serviceName)
        {
            var url = string.Format(CultureInfo.InvariantCulture, "feed/{0}/v1", (serviceName ?? "_default"));
            var key = string.Format(CultureInfo.InvariantCulture, "feed|{0}", (serviceName ?? "_default"));

            routes.Add(key, new ServiceRoute(url, serviceFactory, typeof(Packages))
                               {
                                   Defaults =
                                       new RouteValueDictionary { { "serviceType", "odata" }, { "connectionName", serviceName } },
                                   Constraints = new RouteValueDictionary { { "serviceType", "odata" } }
                               });

            url = string.Format(CultureInfo.InvariantCulture, "feed/{0}/{{action}}/{{id}}/{{version}}", (serviceName ?? "_default"));
            key = string.Format(CultureInfo.InvariantCulture, "feed|{0}|info", (serviceName ?? "_default"));

            routes.MapRoute(key,
                            url,
                            new { controller = "Feed", action = "Index", id = UrlParameter.Optional, version = UrlParameter.Optional });
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            ServiceHostFactory serviceFactory = new DataServiceHostFactory();

            foreach (BuildStoreConnectionSettings tpcSettings in Configuration.BuildStoreSection.Current.Connections)
            {
                RegisterFeed(routes, serviceFactory,
                              tpcSettings.Name == BuildStoreSection.Current.Connections.DefaultConnectionName
                                  ? null
                                  : tpcSettings.Name);
            }

            routes.MapRoute("Download",
                            "feed/{connectionName}/packages/download/{id}",
                            new { controller = "Feed", action = "Download", id = UrlParameter.Optional });

            routes.MapRoute("About",
                            "system/{action}/{id}",
                            new { controller = "System", action = "Index", id = UrlParameter.Optional });

            routes.MapRoute("Project",
                            "project/{action}/{id}",
                            new { controller = "Project", action = "Index", id = UrlParameter.Optional });
            
            routes.MapRoute("Default",
                            "{controller}/{action}/{id}",
                            new { controller = "Home", action = "Index", id = UrlParameter.Optional });
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    }
}
